<template lang="pug">
.list
  .header
    .header-bd
      .header-title 电子拼板模板设置
      .header-btn
        el-button(type="primary" icon="el-icon-plus" size="small" @click="addModal" v-if="$permission('AB-05001-001')") 新建模板
  .puzzle-content
    .puzzle-item(v-for="item in list")
      .puzzle-item-body
        .puzzle-item-handles
          .iconfont.icon-edit(@click="seeDetail(item)" v-if="$permission('AB-05001-002')")
          .iconfont.icon-delete(@click="deleteItem(item)" v-if="$permission('AB-05001-003')")
        .puzzle-item-img(:style="item.preview | filterUrl")
        .puzzle-item-name {{item.title}}
</template>

<script>
import { getModelList, postDeleteModule } from 'api'
export default {
  name: 'templateList',
  data () {
    return {
      page: 1,
      pageSize: 16,
      list: []
    }
  },
  methods: {
    addModal () {
      this.$router.push({ name: 'templateEdit', meta: { appHeader: 'show', lowerSidebar: 'show', appSldebar: 'show', viewHeader: 'show', name: 'puzzle', title: '编辑' } })
    },
    seeDetail (item) {
      this.$router.push({ name: 'templateEdit', params: { id: item.id }, query: { from: 'default' }, meta: { appHeader: 'show', lowerSidebar: 'show', appSldebar: 'show', viewHeader: 'show', name: 'puzzle', title: '编辑' } })
    },
    deleteItem (item) {
      this.$confirm('是否确定删除该模板？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          await this.$axios.post(postDeleteModule, { ids: item.id + '' })
          this.$message.success('删除成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    async getList () {
      try {
        const { ResultSet } = await this.$axios.post(getModelList, { currentPage: this.page, pageLines: this.pageSize })
        this.list = ResultSet.userData.list
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  }
} 
</script>

<style lang="scss" scoped>
.list {
  width: 100%;
  min-height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 28px;
}
.header {
  padding: 0 12px;
  .header-bd {
    padding: 12px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #EDEDED;
  }
  .header-title {
    font-size: 16px;
    line-height: 24px;
    color: #3E4373;
  }
}
.puzzle-content {
  display: flex;
  flex-wrap: wrap;
  margin-top: 24px;
}
.puzzle-item {
  width: 25%;
  padding: 0 12px 24px;
  .puzzle-item-body {
    position: relative;
    width: 100%;
    background: #F7F7FD;
    border-radius: 6px;
    cursor: pointer;
    &:hover {
      .puzzle-item-handles {
        display: flex;
      }
    }
  }
}
.puzzle-item-img {
  width: 100%;
  height: 230px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  border-radius: 6px;
}
.puzzle-item-handles {
  display: none;
  align-items: center;
  justify-content: flex-end;
  position: absolute;
  top: 8px;
  right: 8px;
  .iconfont {
    width: 32px;
    height: 32px;
    color: #fff;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 8px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 6px;
  }
}
.puzzle-item-name {
  padding: 16px;
  font-size: 16px;
  line-height: 24px;
  color: #3E4373;
}
</style>
